package tw.com.book.task01;

import tw.com.book.R;
import tw.com.book.api.task01.MainTaskService;
import tw.com.book.componet.DBContract;
import tw.com.book.core.task01.MainTaskServiceImpl;
import tw.com.book.domain.V001Type;
import tw.com.book.dto.MainDTO;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EditIssueActivity extends Activity {

	private MainTaskService mainTaskService = new MainTaskServiceImpl();

	private final static String SHOW_FROMAT = //
	"%s(%s)/%s:\n" + //
			"%s";//
	private static final String TAG = EditIssueActivity.class
			.getCanonicalName();

	private MainDTO dto = new MainDTO();

	private transient ListView listview;

	private final int REQUEST_CODE = 001;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == REQUEST_CODE) {
			Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();

			this.mainTaskService.doV002s(dto, this);

			this.update();

		}
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void update() {
		IssueAdapter listAdaptet = (IssueAdapter) this.listview.getAdapter();
		listAdaptet.notifyDataSetChanged();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.issue_list);

		this.listview = (ListView) this.findViewById(R.id.issue_history);

		final Bundle bundle = this.getIntent().getExtras();

		final V001Type v001 = (V001Type) bundle
				.getSerializable(DBContract.CREATE_VOO1);

		this.dto.setUuid(v001.getUuid());
		this.dto.setSelectV001(v001);

		this.mainTaskService.doV002s(dto, this);// 取得所有issue 修改歷史,並把第一筆塞進去
												// selectV002

		this.listview.setAdapter(new IssueAdapter());

		this.listview.setOnItemClickListener(new SelectV002());
		this.listview.setOnItemLongClickListener(new SelectLongClick());
	}

	private class SelectLongClick implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {

			dto.setSelectV002(dto.getV002s().get(arg2));// 選到的歷史資料

			Builder myAlertDialog = new AlertDialog.Builder(
					EditIssueActivity.this);

			myAlertDialog.setItems(
					new CharSequence[] { "修改內容","此記錄刪除", "此歷史紀錄更新為最新" },
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								
								mainTaskService.deleteSubV002(dto,
										EditIssueActivity.this);
								update();
								break;

							case 1:
						
								mainTaskService.setMaterV002(dto, EditIssueActivity.this);
								update();
								break;
							}
							// TODO Auto-generated method stub

						}

					});
			myAlertDialog.show();

			// TODO Auto-generated method stub
			return false;

		}

	}

	private class SelectV002 implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {

			Intent it = new Intent(EditIssueActivity.this,
					PendContentActivity.class);

			final Bundle bundle = new Bundle();
			bundle.putSerializable(DBContract.V002_TABLE, dto.getSelectV002());// 傳遞v002-->PendActivity
			it.putExtras(bundle);

			startActivityForResult(it, REQUEST_CODE);

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.edit_action:
			Intent it = new Intent(EditIssueActivity.this,
					PendContentActivity.class);

			final Bundle bundle = new Bundle();
			bundle.putSerializable(DBContract.V002_TABLE, dto.getSelectV002());// 傳遞v002-->PendActivity
			it.putExtras(bundle);

			startActivityForResult(it, REQUEST_CODE);
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private class IssueAdapter extends BaseAdapter {
		private LayoutInflater mInflater = null;

		public IssueAdapter() {
			this.mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return dto.getV002s().size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return dto.getV002s().get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {

			View view = mInflater.inflate(R.layout.issue_view, null);

			TextView content = (TextView) view.findViewById(R.id.issu_view);
			content.setText(dto.getV002s().get(arg0).getContent());
			// TODO Auto-generated method stub
			return view;
		}
	}
}
